package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/student")
public class GetReleaseCourseController extends BaseController {

	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;

	private static Logger logger = LoggerFactory
			.getLogger(GetReleaseCourseController.class);

	/**
	 * 查询课程
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/GetReleaseCourse", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getReleaseCourse(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();

		try {
			String PageNum = request.getParameter("PageNum");
			String PageSize = request.getParameter("PageSize");

			if (PageNum != null && !PageNum.isEmpty() || PageSize != null
					&& !PageSize.isEmpty()) {
				if (PageNum.equals("0") || PageSize.equals("0")) {
					response.setErrorcode(1);
					response.setMessage("页码或页数不能为0...");
					return response.toJSON();
				}
			}

			// 课程开始时间
			String beginDate = request.getParameter("beginDate");
			// 课程结束时间
			String endDate = request.getParameter("endDate");
			// 教练ID
			String teacherId = request.getParameter("teacherId");
			// 科目
			String subjectid = request.getParameter("subjectid");
			// 学员ID
			String studentId = request.getParameter("studentId");
			//学驾类型
			String drivingType = request.getParameter("drivingType");
			//学员分校ID
			String branchSchoolId = request.getParameter("branchSchoolId");
			//学员总校ID
			String driveSchoolId = request.getParameter("driveSchoolId");
			//学员班型ID
			String classInfoId = request.getParameter("classInfoId");
			// 筛选教练ID
			String teaId = request.getParameter("teaId");
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			if ((null != beginDate && !beginDate.isEmpty())
					&& (null != endDate && !endDate.isEmpty())
					&& (null != studentId && !studentId.isEmpty())
					&& (null != branchSchoolId && !branchSchoolId.isEmpty())
					&& (null != driveSchoolId && !driveSchoolId.isEmpty())
					&& (null != classInfoId && !classInfoId.isEmpty())) {
				if(studentId.equals(0)){
					response.setErrorcode(1);
					response.setMessage("该学员未绑定，不可预约课程，请先绑定！！");
					return response.toJSON();
				}
				paramsMap.put("PageNum", PageNum);
				paramsMap.put("PageSize", PageSize);
				paramsMap.put("imgUrl", Config.getImageAddress());
				paramsMap.put("teacherId", teacherId);
				paramsMap.put("subjectid", subjectid);
				paramsMap.put("beginDate", beginDate);
				paramsMap.put("endDate", endDate);
				paramsMap.put("studentId", studentId);
				paramsMap.put("drivingType", drivingType);
				paramsMap.put("branchSchoolId", branchSchoolId);
				paramsMap.put("driveSchoolId", driveSchoolId);
				paramsMap.put("classInfoId", classInfoId);
				paramsMap.put("teaId", teaId);
				response = bizService.getReleaseCourse(paramsMap,response);
			} else {
				response.setErrorcode(1);
		    	response.setMessage("必填参数为空！");
			}
		} catch (Exception e) {
			logger.info("GetReleaseCourse" + e.toString());
			response.setReturnMsg(1, "获取课程数据异常！");
		} finally {
			return response.toJSON();
		}
	}


	/**
	 * 课程页面 教练端
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getReleaseCourseListInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getReleaseCourseListInfo(
			HttpServletRequest request) throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {

			String date = request.getParameter("date");
			String teacherId = request.getParameter("teacherId");
			Map<String, Object> valMap = new HashMap<String, Object>();
			if (date != null && !date.isEmpty() && teacherId != null
					&& !teacherId.isEmpty()) {
				String imgUrl = Config.getImageAddress();
				valMap.put("imgUrl", imgUrl);
				valMap.put("teacherid", teacherId);
				valMap.put("date", date);
				List<Map<String, Object>> stumap = bizDao
						.GetTecReleaseCourse(valMap);
				if (null != stumap && stumap.size() > 0) {
					for (Map<String, Object> map : stumap) {
						map.put("studentinfo", bizDao.GetTecRAppointmentInfo(
								Integer.parseInt(map.get("id").toString()),
								imgUrl));
					}

					response.setData(stumap);
				} else {
					response.setErrorcode(1);
					response.setMessage("无课程数据");
				}
			} else {
				response.setErrorcode(1);
				response.setMessage("教练编号或月份不能为空...");
			}

		} catch (Exception e) {
			logger.info("getReleaseCourseListInfo:" + e.toString());
			response.setReturnMsg(1, "操作失败！");
		} finally {
			return response.toJSON();
		}
	}

}
